package com.example.recordingpen_datacollection.Utils.http;

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HuoSanHttpUtil {
    public static String getZJData(String BDurl,String token,String role,String content) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS) // 设置连接超时时间为30秒
                .readTimeout(120, TimeUnit.SECONDS) // 设置读取超时时间为60秒
                .writeTimeout(120, TimeUnit.SECONDS) // 设置写入超时时间为60秒
                .build();

        List<Map<String,String>> maps = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("role","system");
        map.put("content","你是一个会议总结助手");
        maps.add(map);
        Map<String,String> map2 = new HashMap<>();
        map2.put("role",role);
        map2.put("content",content);
        maps.add(map2);
        Map<String,Object>  aliYunJsonRoot = new HashMap<String,Object>();
        aliYunJsonRoot.put("model","ep-20240516011401-g68qw");
        aliYunJsonRoot.put("messages",maps);
        aliYunJsonRoot.put("stream",false);

        String jsonBody = JSON.toJSONString(aliYunJsonRoot);;

        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        Request request = new Request.Builder()
                .addHeader("Content-Type","application/json")
                .addHeader("Authorization","Bearer "+token)
                .url(BDurl)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                return responseData;
            } else {
                return response.message();
//                System.out.println("Request failed: " + ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
